package com.fxc;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.lang.reflect.InvocationTargetException;
import java.util.Random;

import com.fxc.stragegy.DefaultFireStrategy;
import com.fxc.stragegy.FireStrategy;
import com.fxc.stragegy.FourDirFireStragegy;
import com.fxc.tank.abstractfactory.BaseTank;

public class Tank extends BaseTank{
	
	public int x, y;

	public Dir dir = Dir.DOWN;
	
	private static final int SPEED = Integer.parseInt((String)PropertyManager.get("tankSpeed"));
	
	private boolean moving = true;
	
	public TankFrame tf = null;
	
	private Random random = new Random();

	public Group frGroup = Group.BAD;
	
	public Rectangle rect = new Rectangle();
	
	FireStrategy fs;
	

	// 坦克的大小
	public static final int WIDTH  = ResourceManager.p1tankL.getWidth();
	
	public static final int HEIGHT = ResourceManager.p1tankL.getHeight();
	
	// tank存活的属性
	public boolean Liveing = true;
	
	public Group getFrGroup() {
		return frGroup;
	}
	
	public void setFrGroup(Group frGroup) {
		this.frGroup = frGroup;
	}


	public Tank(int x, int y, Dir dir, TankFrame tf,  Group frGroup) {
		super();
		this.x = x;
		this.y = y;
		this.dir = dir;
		this.tf = tf;
		this.frGroup = frGroup;
		
		rect.x = this.x;
		rect.y = this.y;
		rect.width = WIDTH;
		rect.height = HEIGHT;
		
		if (frGroup == Group.GOOD) {
			// fs = new FourDirFireStragegy();
			String goodfsName = (String)PropertyManager.get("goodFS");
			

				try {
					fs = (FireStrategy)Class.forName(goodfsName).getDeclaredConstructor().newInstance();
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			
		}else {
			fs = new DefaultFireStrategy();
		}
	}

	public void paint(Graphics g) {
		
		// 如果坦克死了就不画出来。
		if (!Liveing) {
			tf.tanks.remove(this);
		}
		
//		Color color = g.getColor();
//		g.setColor(color.yellow);
//		g.fillRect(x, y,50, 50);
//		g.setColor(color);
		
		switch (dir) {
		case UP:
			g.drawImage(this.frGroup == Group.GOOD ? ResourceManager.p1tankU : ResourceManager.p2tankU, x, y, null);
		break;
		case LEFT:
			g.drawImage(this.frGroup == Group.GOOD ? ResourceManager.p1tankL : ResourceManager.p2tankL, x, y, null);
		break;
		case RIGHT:
			g.drawImage(this.frGroup == Group.GOOD ? ResourceManager.p1tankR : ResourceManager.p2tankR, x, y, null);
		break;
		case DOWN:
			g.drawImage(this.frGroup == Group.GOOD ? ResourceManager.p1tankD : ResourceManager.p2tankD, x, y, null);
		break;

		default: 
			break;
		}
		
		move();
	}

	// 移动
	private void move() {
		// 判断坦克是否在移动
		if (!moving) return;
		
		switch (dir) {
		case LEFT:
			x -= SPEED;
			break;
		case UP:
			y -= SPEED;
			break;
		case RIGHT:
			x += SPEED;
			break;
		case  DOWN:
			y += SPEED;
			break;

		default:
			break;
		}
		
		if (this.frGroup == Group.BAD && random.nextInt(10) > 8) {
			this.fire();
		}
		
		if (this.frGroup == Group.BAD && random.nextInt(100) > 95) {
			randomDir();			
		}
		
		boundCheck();
		
		// update rect
		rect.x = this.x;
		rect.y = this.y;
	}
                         
	/**
	 * 坦克碰撞到边界停止
	 */
	private void boundCheck() {
		if (this.x < 2) {
			x = 2;
		}
		if (this.y < 28 ) {
			y = 28;
		}
		if (this.x > TankFrame.GMAE_WIDTH - Tank.WIDTH - 2) {
			x = TankFrame.GMAE_WIDTH - Tank.WIDTH - 2;
		}
		if (this.y > TankFrame.GAME_HEIGHT - Tank.HEIGHT - 2) {
			y = TankFrame.GAME_HEIGHT - Tank.WIDTH - 2;
		}
	}

	private void randomDir() {
		this.dir = Dir.values()[random.nextInt(4)];
		
	}       

	public boolean isMoving() {
		return moving;
	}

	public void setMoving(boolean moving) {
		this.moving = moving;
	}

	public Dir getDir() {
		return dir;
	}
    
	public void setDir(Dir dir) {
		this.dir = dir;
	}

	public static int getSpeed() {
		return SPEED;
	}

	// 开火
	public void fire() {
		
//		// 计算坦克的中心位置
//		// 有待完善，子弹应从坦克正前方炮筒射出。
//		int tX = this.x  + Tank.WIDTH/2 - Bullet.WIDTH/2;
//		
//		int tY = this.y  + Tank.HEIGHT /2- Bullet.HEIGHT/2;
//		
//		System.out.println(tX);
//		System.out.println(tY);
//		
//		tf.bullets.add(new Bullet(tX, tY, this.dir, this.tf, this.frGroup));
		fs.fire(this);
		
	}

	public void die() {
		this.Liveing = false;
	}
	
	
}
